package com.zhenhao.xsq.ui.activity.door;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhenhao.xsq.R;
import com.zhenhao.xsq.adapter.door.DoorManageAdapter;
import com.zhenhao.xsq.application.AppConfig;
import com.zhenhao.xsq.base.BaseActivity;
import com.zhenhao.xsq.ui.activity.door.component.DaggerDoorManageComponent;
import com.zhenhao.xsq.ui.activity.door.contract.DoorManageContract;
import com.zhenhao.xsq.ui.activity.door.module.DoorManageModule;
import com.zhenhao.xsq.ui.activity.door.presenter.DoorManagePresenter;
import com.zhenhao.xsq.utils.TimeUtil;
import com.zhenhao.xsq.utils.ToastUtil;
import com.zhenhao.xsq.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 智能门禁
 *
 * @author William
 * @Package com.yxld.yxchuangxin.xsq.ui.activity.menjin
 * @Description: $description
 * @date 2020/05/28 15:25:29
 */

public class DoorManageActivity extends BaseActivity implements DoorManageContract.View {

    @Inject
    DoorManagePresenter mPresenter;

    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    @BindView(R.id.tv_address)
    TextView tvAddress;

    private DoorManageAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_door_manage);
        ButterKnife.bind(this);
        //        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        setToolbarTitle("智能门禁");

        autolayout.setBackgroundColor(ContextCompat.getColor(this, R.color.color_00000000));
        mToolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.color_00000000));
        rootLayout.setBackground(ContextCompat.getDrawable(this, R.mipmap.bg_activity_base));
        toolbarTitle.setTextColor(ContextCompat.getColor(this, R.color.black));
        toolbarMenu.setTextColor(ContextCompat.getColor(this, R.color.color_333333));
        mToolbar.setNavigationIcon(ContextCompat.getDrawable(this, R.mipmap.toolbar_back2));

        initRv();
    }


    private void initRv() {
        UIUtils.configRecycleView(rv, new LinearLayoutManager(this));
        adapter = new DoorManageAdapter();
        rv.setAdapter(adapter);

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
//                DoorInfo.ListBean.DataBean listBean =
//                        (DoorInfo.ListBean.DataBean) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.tv_open://开门
                        ToastUtil.showLong("开门");
                        if (TimeUtil.isFastClick()) {
//                            DoorInfo.ListBean.DataBean listBean = (DoorInfo.ListBean.DataBean)
//                            adapter.getData()
// .get(position);
//                            EvenBusMsg evenBusMsg = new EvenBusMsg("HomeService", listBean
//                            .getMac(), "opendoor");
//                            EventBus.getDefault().post(evenBusMsg);
//                            if (listBean.getXdoorType() == 0) {
//                                Map<String, String> map = new HashMap<>();
//                                map.put("xdoorBH", "" + listBean.getXdoorBh());//门禁机编号
//                                mPresenter.kaiMenBle(map);
//                            } else if (listBean.getXdoorType() == 1 || listBean.getXdoorType()
//                            == 3) {
//                                Map<String, String> map = new HashMap<>();
//                                map.put("mac", "" + listBean.getMac());//mac地址
//                                mPresenter.kaiMenFace(map);
//                            }
                        } else {
                            ToastUtil.showLong("点击频率过快");
                        }
                        break;
                }
            }
        });

        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                srl.finishRefresh();
                srl.resetNoMoreData();

                adapter.removeAllHeaderView();
                initData();
            }
        });
    }

    @Override
    protected void initData() {

//        adapter.removeAllHeaderView();

//        BleEntity bleInfoJson = SpSaveUtils.getBleInfoJson();
//        if (null != bleInfoJson && null != bleInfoJson.getList() && null != bleInfoJson.getList
//        ().getData() &&
//                bleInfoJson.getList().getData().size() > 0) {
//            tvFamilyCode.setVisibility(View.VISIBLE);
//        } else {
//            tvFamilyCode.setVisibility(View.GONE);
//        }
//
//        int yezhuFwgx = fwInfoJson.getYezhuFwgx();
//        if (yezhuFwgx == 1) {
//            //第一产权人 有权限修改
//            tvFamilyCode.setVisibility(View.VISIBLE);
//        } else {
//            tvFamilyCode.setVisibility(View.GONE);
//        }
//
//        if (null != fwInfoJson && null != yzInfoJson) {
//            Map<String, String> map = new HashMap<>();
//            map.put("loudong", "" + fwInfoJson.getLoudong());//楼栋
//            map.put("danyuan", "" + fwInfoJson.getDanyuan());//单元
//            map.put("qiqu", "" + fwInfoJson.getQiqu());//期区
//            map.put("xiangmuBh", "" + yzInfoJson.getXiangmuBh());//项目编号
//            map.put("fangwuId", "" + SpSaveUtils.getFwId());//房屋id
//            mPresenter.getDoorList(map);
//        }
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("1");
        list.add("1");
        adapter.setNewData(list);

    }


    @Override
    protected void setupActivityComponent() {
        DaggerDoorManageComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .doorManageModule(new DoorManageModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(DoorManageContract.DoorManageContractPresenter presenter) {
        mPresenter = (DoorManagePresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }


}